﻿//消息通知初始化
//
var Messagetoastr = (function (toolbox) {
    return toolbox;
})(Messagetoastr || {});

Messagetoastr.TypeShow = (function (MessageTypeShow) {
    var self = MessageTypeShow;
    self.message = function (type, message) {
        toastr.options = {
            "closeButton": false, //是否显示关闭按钮
            "debug": false, //是否使用debug模式
            "positionClass": "toast-top-center",//弹出窗的位置
            "showDuration": "1000",//显示的动画时间
            "hideDuration": "1000",//消失的动画时间
            "timeOut": "5000", //展现时间
            "extendedTimeOut": "1000",//加长展示时间
            "showEasing": "swing",//显示时的动画缓冲方式
            "hideEasing": "linear",//消失时的动画缓冲方式
            "showMethod": "fadeIn",//显示时的动画方式
            "hideMethod": "fadeOut" //消失时的动画方式
        };
        toastr[type](message);

    };
    self.success = function (message) {
        self.message("success", message);
    };
    self.info = function (message) {
        self.message("info", message);
    };

    self.warning = function (message) {
        self.message("warning", message);
    };

    self.error = function (message) {
        self.message("error", message);
    };

    return self;
})(Messagetoastr.TypeShow || {});

//form提交表单统一在这里
//使用ajax异步提交
var form = $(".js-form-submit");
form.submit(function () {

    var $self = $(this)
    var formUrl = $self.attr("action");
    var formType = $self.attr("method");
    var formMimeType = $self.attr("enctype");
    var formData = new FormData(this);

    var ajaxoption = {
        url: formUrl,
        type: formType,
        data: formData,
        processData: false,
        contentType: false,
        error: function () {
            Messagetoastr.TypeShow.error("server error！");
        },
        success: function (result) {
            if (formMimeType) {
                result = JSON.parse(result);
            }
            if (result.Messages && result.Messages instanceof Array) {
                if (result.Success === true) {
                    for (var i = 0 ; i < result.Messages.length; i++) {
                        Messagetoastr.TypeShow.info(result.Messages[i]);
                    }
                }
                else {
                    for (var i = 0 ; i < result.Messages.length; i++) {
                        Messagetoastr.TypeShow.warning(result.Messages[i]);
                    }
                }
            }
            var redirectUrl = "";
            if (result.ReloadPage) {
                redirectUrl = window.location.href;
            }
            else if (result.RedirectUrl) {
                redirectUrl = result.RedirectUrl;
            }

            if (redirectUrl) {
                setTimeout(function () {
                    window.location.href = redirectUrl;
                }, 800);
            }
        }
    };

    if (formMimeType) {
        ajaxoption.mimeType = formMimeType;
    }


    $.ajax(ajaxoption);
    return false;
})